<?php include (dirname(__FILE__)."/../include/header.php"); ?>
<?php if(!isset($_SESSION['_user'])){header("location:../user_login.php");}?>
<?php
	//新增
	$method="insert";
	if($_REQUEST['method']=='insert'){
		$user_id = $_SESSION['_user']['user_id'];
		$username = $_SESSION['_user']['username'];
		$address = $_REQUEST['address'];
		$phone = $_REQUEST['phone'];
		$tel = $_REQUEST['tel'];
		if($address!="" && $phone!=""){
		$sql="insert into zy_address(user_id,address,tel,phone,create_user,create_time,update_user,update_time) " .
				"values('$user_id','$address','$tel','$phone','$username',now(),'$username',now())";
		mysql_query($sql);
		header("location:address_list.php");
		$method="update";
		}else{
			$error="地址或手机不能为空";
		}
	}
	//查询单条信息
	if($_REQUEST['method']=='detail'){
		$sql="select zy_address.* from zy_address where add_id=".$_REQUEST['add_id'];
		$result=mysql_query($sql);
		$result=mysql_fetch_array($result);
		$method="update";
	}
	//修改
	if($_REQUEST['method']=='update'){
		$user_id = $_SESSION['_user']['user_id'];
		$username = $_SESSION['_user']['username'];
		$address = $_REQUEST['address'];
		$phone = $_REQUEST['phone'];
		$tel = $_REQUEST['tel'];
		if($address!="" && $phone!=""){
		$sql="update zy_address set address='$address',tel='$tel',phone='$phone',create_user='$username',create_time=now(),update_user='$username',update_time=now() where add_id=".$_REQUEST['add_id'];
		mysql_query($sql);
		header("location:address_list.php");
		}else{
			$error="地址或手机不能为空";
		}
}
	//删除
	if($_REQUEST['method']=='delete')
	{
		$add_id = $_REQUEST['add_id'];
		$sql="delete from zy_address where add_id=".$add_id;
		mysql_query($sql);
		header("location:address_list.php");
	}
	//设置为默认地址
	if($_REQUEST['method']=='defaultAddress')
	{
		/**
		 * 思想:先把当前用户下默认地址字段(is_default)更新为0,然后在把要设置的地址(is_default)字段更新为1
		 */
		$username = $_SESSION['_user']['username'];
		$sql="update zy_address set is_default = 0,update_user='$username',update_time=now() where user_id=".$_SESSION['_user']['user_id'];
		mysql_query($sql);
		$sql="update zy_address set is_default = 1,update_user='$username',update_time=now() where add_id=".$_REQUEST['add_id'];
		mysql_query($sql);
		header("location:address_list.php");
	}
?>
<div class="container">
    <div class="row">
    <div class="span12">
           <!-- <div class="alert alert-warning-new">
          <span class="alert-radius-left"></span>
                      <span class="alert-star"></span>
            <span class="alert-shadow"></span>
            您的地址已经保存                    <a class="alert-close"></a>
          <span class="alert-radius-right"></span>
        </div>-->
      </div>
    </div>
          
	<ul class="breadcrumb">
       <li><a href="<?php echo SITE_ROOT; ?>">返回首页</a><span class="divider">/</span></li>
       <li><a href="<?php echo SITE_ROOT."/user/address_list.php"; ?>">我的地址</a></li>
      </ul>

<div class="row">
    <div class="span12 wash-paper profile">
        <div class="row">
        <div class="span3">
<?php include (dirname(__FILE__)."/../include/user/left.php"); ?>
            <div class="span9">
                <div class="main-content">
                    
<div class="content-header">
  <h3>管理我的地址</h3>
</div>

<div class="content-inner address">
      <h4>已经保存的送餐地址</h4>
    <table class="address-table table table-bordered">
      <thead>
        <tr>
          <th class="col-address">地址</th>
          <th class="col-phone">手机</th>
          <th class="col-phone-bk">备选电话</th>
          <th class="col-action">操作</th>
        </tr>
      </thead>
      <tbody>
      <?php
		//查询已保存的送餐地址
		$sql = "SELECT zy_address.* FROM zy_address WHERE user_id=".$_SESSION['_user']['user_id']." order by add_id desc";
		$result2 = mysql_query($sql);
		while($res = mysql_fetch_array($result2)){
		?>
            <tr class="stable_address">
            <td class="col-address"><?php echo $res['address'];?></td>
            <td class="col-phone"><?php echo $res['phone']?></td>
            <td class="col-phone-bk"><?php echo $res['tel']?></td>
            <td class="col-action"><span><a href="address_list.php?method=defaultAddress&add_id=<?php echo $res['add_id'];?>"><?php if($res['is_default'] == 1){ echo "默认地址";}else{echo "设为默认地址";}?></a></span>| <a href="address_list.php?method=detail&add_id=<?php echo $res['add_id'];?>">编辑</a>
            | <a class="gray_link" href="address_list.php?method=delete&add_id=<?php echo $res['add_id'];?>">删除</a>
            </td>
          </tr>
          <?php }?>
              </tbody>
    </table>
  
  <h4>新增/编辑送餐地址</h4>
  <form class="form-horizontal" name="address" id="add_address_form" method="post" action="address_list.php?method=<?php echo $method;?>" >
  <div class="field_line" ><font color="red"><?php echo $error;?></font></div>
    <fieldset>
      <input type="hidden" name="address[id]" id="address_id" />      <div class="control-group">
        <label class="control-label" for="address[address]"><span class="required">*</span>详细地址</label>
        <div class="controls">
          <input type="text" name="address" class="span4" id="address_address" value="<?php echo $result['address'];?>"/>          <p class="help-block">(请填详细一点)</p>
          <input type="hidden" name="add_id" id="add_id" value="<?php echo $result['add_id'];?>">
        </div><!--end input-->
      </div><!--end clearfix-->

      <div class="control-group">
        <label class="control-label" for="address[phone]"><span class="required">*</span>手&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;机</label>
        <div class="controls">
          <input type="text" name="phone" class="span4" id="address_phone" value="<?php echo $result['phone'];?>"/>          <p class="help-block">(外卖到时将以该电话通知您)</p>
        </div><!--end input-->
      </div><!--end clearfix-->

      <div class="control-group">
        <label class="control-label" for="address[phone]">备选电话</label>
        <div class="controls">
          <input type="text" name="tel" class="span4" id="address_phone_bk" value="<?php echo $result['tel'];?>"/>          <p class="help-block">(为方便送餐员联系，请再留一个备用电话)</p>
        </div><!--end input-->
      </div><!--end clearfix-->

      <div class="form-actions">
        <input type="submit" id="add_address_submit" name="submit" class="btn btn-yellow" value="保存"/>
      </div>
    </fieldset>

  </form>
</div>

               </div>
            </div>
        </div>
    </div>
</div>

                      <a id="back-to-top" href="javascript:void(0);">
              <div class="backtotop"></div>
            </a>
                  </div>
<?php include (dirname(__FILE__)."/../include/footer.php"); ?>